package com.maistars.spider.service.render.handler.impl;

import com.maistars.spider.common.util.HtmlUtil;
import com.maistars.spider.infrastructure.adapter.JsoupAdapter;
import com.maistars.spider.service.render.handler.HtmlHandler;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @author dhr
 * @date 2021/11/21 下午4:35
 */
public class CleanHtmlHandler extends HtmlHandler {

    public CleanHtmlHandler(Map<String, String> context) {
        super(context);
    }

    @Override
    protected void invoke() {
        // 移除script标签
        String handleHtml = HtmlUtil.removeScriptTag(context.get("handledHtml"));
        // 移除指定的class
        String selectorList = context.get("removeElementSelectorList");
        if(StringUtils.isNotBlank(selectorList)) {
            String[] selectors = StringUtils.split(selectorList,"\n");
            handleHtml = JsoupAdapter.removeElementsBySelector(handleHtml, selectors);
        }
        context.put("handledHtml",handleHtml);
    }
}
